package cn.tedu.mvccoolshark.controller;

import cn.tedu.mvccoolshark.mapper.ProductMapper;
import cn.tedu.mvccoolshark.pojo.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/v1/product/")
public class ProductController {
    @Autowired
    private ProductMapper productMapper;

    @RequestMapping("insert")
    @ResponseBody
    public String insertProduct(Product product) {
        productMapper.insert(product);
        return "添加成功";
    }

    @RequestMapping("select")
    @ResponseBody
    public String selectProduct(){
        List<Product> list=productMapper.select();
        String html = "<table border='1'>";
        html += "<caption>商品列表</caption>";
        html += "<tr><th>id</th><th>标题</th><th>价格</th><th>库存</th><th>操作</th></tr>";
        for (Product p: list) {
            html += "<tr>";
            html += "<td>" + p.getId() + "</td>";
            html += "<td>" + p.getTitle() + "</td>";
            html += "<td>" + p.getPrice() + "</td>";
            html += "<td>" + p.getNum() + "</td>";
            html += "<td><a href='/v1/product/delete?id=" + p.getId() + "'>删除</a>" + "</td>";
            html += "</tr>";
        }
        html += "</table>";
        return html;
    }

    @RequestMapping("delete")
    @ResponseBody
    public String deleteProduct(){
        productMapper.delete(1);
        return "删除成功";
    }

    @RequestMapping("update")
    @ResponseBody
    public String updateProduct(Product product){
        productMapper.update(product);
        return "修改成功!";
    }
}
